package com.kunbo.cn.controller.core.inventory;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.kunbo.cn.dao.core.inventory.InventoryDao;
import com.kunbo.cn.dao.system.user.DepartmentDao;
import com.kunbo.cn.entity.core.inventory.Inventory;
import com.kunbo.cn.entity.core.sale.OrderCarryOver;
import com.kunbo.cn.entity.system.user.Department;
import com.kunbo.cn.service.core.inventory.InventoryService;
import com.kunbo.cn.service.core.sale.OrderCarryOverService;
import com.kunbo.cn.service.system.user.DepartmentService;
import com.kunbo.cn.util.date.SDate;
import com.kunbo.cn.util.json.Json;
import com.kunbo.cn.util.retu.BuildBean;
import com.kunbo.cn.util.retu.CommonResult;
import com.kunbo.cn.util.retu.ResultBean;
import com.kunbo.cn.util.retu.ReturnBean;
/**
 *库存管理
 * @author bjl  
 * @date 2019年2月26日 
 */

@RestController  
@RequestMapping("/Inventory/*")  
public class InventoryController {
	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		@Autowired
	    private JdbcTemplate jdbcTemplate;
		@Autowired
		private InventoryService service;
		
		/**
		 * 添加库存单
		 * 
		 */
		@PostMapping("/add")
		public CommonResult add(Inventory inventory) {
			service.save(inventory);
			return new CommonResult(true,"操作成功",null);
		}
		
		/**
		 * 生成库存编码
		 * 
		 */
		/*@GetMapping("/createCode")
		public String createCode(){
			 String prex = "KCD";
			 String suffix="1001";
			 int n=0;
				String sql="";
				sql +="select\r\n" ; 
				sql +=" MAX(autonomy) as au\r\n";
				sql +="FROM business_inventory_manage d\r\n";
				List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
				Integer t = (Integer) list.get(0).get("au");
				//Float lowerAmount=t-t1;
				String inventoryCode="";
				String time =sdf.format(new Date());
				if(t==null){
					System.out.println("***********tare**********");
					inventoryCode=prex+time+suffix;
				}else {
					int f=t+1;
					inventoryCode=prex+time+f;
				}
			return inventoryCode;
		}*/
		@GetMapping("/createCode")
		public String createCode(){
			 String prex = "KC";
			 String noticeCode= prex+SDate.getSystemDateYMDHMSToString();
			return noticeCode;
		}
		//暂时不用
	/*	@PutMapping("/update")
		public CommonResult update(Inventory inventory) {
			service.update(inventory);
			return new CommonResult(true,"操作成功",null);
		}*/
		
		/**
		 *修改审核状态 
		 * 
		 *//*
		@PutMapping("/updateState")
		public CommonResult update1(Inventory carryOver) {
			service.updateExamineState(carryOver);
			return new CommonResult(true,"操作成功",null);
		}*/
		
		@DeleteMapping("/delete")
		public CommonResult delete(String id) {
			service.delete(id);
			return new CommonResult(true,"操作成功",null);
		}
		
		@GetMapping("/findById")
		public Inventory findById(String id) {
			Inventory d = service.findById(id);
			return d;
		}
		
		/**
		 *条件分页查询
		 * 
		 */
		@GetMapping("/findInventory")
		public Page<Inventory> findInventory(String materType,String curentDate,Integer pageNo,Integer pageSize) {
			Page<Inventory> page = service.findInventory(materType,curentDate,pageNo, pageSize);
			return page;
		}
		/**
		 *条件分页查询
		 * 
		 */
		@GetMapping("/findInventorys")
		public List<Inventory> findInventorys(String material,Integer pageNo,Integer pageSize) {
			List<Inventory> page = service.findInventorys(material,pageNo, pageSize);
			return page;
		}
		/**
		 * 获取不同物料类型的list
		 * @param code
		 * @param materialType
		 * @param page
		 * @param pageSize
		 * @return
		 */
		@PostMapping("/list")
		public ResultBean<List<Map<String,Object>>> list(
				@RequestParam("material")String material,
				//@RequestParam("materialType")String materialType,
				@RequestParam("pageNo")Integer pageNo,
				@RequestParam("pageSize")Integer pageSize
				){
			ResultBean<List<Map<String,Object>>> result = new ResultBean<List<Map<String,Object>>>();
			return BuildBean.buildResult(result, "success", "error", service.list(material,pageNo,pageSize));
		}
}
